package com.wuxx.quartz.trigger;

import com.wuxx.quartz.model.TimingModel;
import org.nutz.ioc.loader.annotation.IocBean;
import org.quartz.Trigger;

import java.util.ArrayList;
import java.util.List;


/**
 * @author wuxx
 */
@IocBean(name = "triggerManager",args = {"refer:cronTrigger","refer:intervalTrigger"})
public class TriggerManager {


   protected List<ITriggerFactory> triggerFactories;

    public TriggerManager(ITriggerFactory args,ITriggerFactory args1) {
        List<ITriggerFactory> list = new ArrayList<>();
        list.add(args);
        list.add(args1);
        this.triggerFactories = list;
    }

    /**
     * 生成对应的触发器
     *
     * @param timingModel 触发器model
     * @return org.quartz.Trigger
     * @author YuanXiaohan
     * @date 2021/12/16 2:53 下午
     */
    public Trigger build(TimingModel timingModel) {
        for (ITriggerFactory triggerFactory : triggerFactories) {
            if (triggerFactory.check(timingModel.getType())) {
                return triggerFactory.build(timingModel);
            }
        }
        return null;
    }

    public List<ITriggerFactory> getTriggerFactories() {
        return triggerFactories;
    }

    public void setTriggerFactories(List<ITriggerFactory> triggerFactories) {
        this.triggerFactories = triggerFactories;
    }
}
